package com.huizu.project.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.huizu.common.core.domain.entity.SysUser;
import com.huizu.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.huizu.common.annotation.Log;
import com.huizu.common.core.controller.BaseController;
import com.huizu.common.core.domain.AjaxResult;
import com.huizu.common.enums.BusinessType;
import com.huizu.project.domain.ProUserTer;
import com.huizu.project.service.IProUserTerService;
import com.huizu.common.utils.poi.ExcelUtil;
import com.huizu.common.core.page.TableDataInfo;

/**
 * 教师用户Controller
 * 
 * @author zhangmx
 * @date 2024-05-25
 */
@RestController
@RequestMapping("/project/prouserter")
public class ProUserTerController extends BaseController
{
    @Autowired
    private IProUserTerService proUserTerService;
    @Autowired
    private ISysUserService sysUserService;
    /**
     * 查询教师用户列表
     */
    @PreAuthorize("@ss.hasPermi('project:prouserter:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProUserTer proUserTer)
    {
        startPage();
        List<ProUserTer> list = proUserTerService.selectProUserTerList(proUserTer);
        return getDataTable(list);
    }

    /**
     * 查询教师用户列表
     */
    //@PreAuthorize("@ss.hasPermi('project:prouserter:list')")
    @GetMapping("/listTotal")
    public TableDataInfo listTotal(ProUserTer proUserTer)
    {
        List<ProUserTer> list = proUserTerService.selectProUserTerList(proUserTer);
        return getDataTable(list);
    }

    /**
     * 导出教师用户列表
     */
    @PreAuthorize("@ss.hasPermi('project:prouserter:export')")
    @Log(title = "教师用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProUserTer proUserTer)
    {
        List<ProUserTer> list = proUserTerService.selectProUserTerList(proUserTer);
        ExcelUtil<ProUserTer> util = new ExcelUtil<ProUserTer>(ProUserTer.class);
        util.exportExcel(response, list, "教师用户数据");
    }

    /**
     * 获取教师用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:prouserter:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") String userId)
    {
        return success(proUserTerService.selectProUserTerByUserId(userId));
    }

    /**
     * 新增教师用户
     * 2024-8-17 修改：同时sys_user表插入新数据
     */
    @PreAuthorize("@ss.hasPermi('project:prouserter:add')")
    @Log(title = "教师用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProUserTer proUserTer)
    {
        return toAjax(proUserTerService.insertProUserTer(proUserTer));
    }

    /**
     * 修改教师用户
     *2024-8-17 修改：sys_user表同步更新数据
     */
    @PreAuthorize("@ss.hasPermi('project:prouserter:edit')")
    @Log(title = "教师用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProUserTer proUserTer)
    {
        int row = 0;
        row = proUserTerService.updateProUserTer(proUserTer);
        SysUser sysUser = new SysUser();
        sysUser = sysUserService.selectUserByUserName(proUserTer.getUserId());
        sysUser.setDeptId(Long.parseLong(proUserTer.getXyId()));
        sysUser.setPhonenumber(proUserTer.getPhone());
        sysUser.setNickName(proUserTer.getName());
        sysUserService.updateUser(sysUser);
        return toAjax(row);
    }

    /**
     * 修改用户管理
     * 2024-8-17 修改：sys_user表同步修改密码,修改ProUserTerServiceImpl.java  resetPassword方法
     */
    @PreAuthorize("@ss.hasPermi('project:prouserter:edit')")
    @Log(title = "教师用户", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPassword")
    public AjaxResult resetPassword(@RequestBody ProUserTer proUser)
    {
        return toAjax(proUserTerService.resetPassword(proUser));
    }

    /**
     * 删除教师用户
     */
    @PreAuthorize("@ss.hasPermi('project:prouserter:remove')")
    @Log(title = "教师用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable String[] userIds)
    {
        int row = 0;
        try {
            row = proUserTerService.deleteProUserTerByUserIds(userIds);
            row = sysUserService.deleteUserByUserNames(userIds);
        }catch (Exception e){
            e.printStackTrace();
        }

        return toAjax(row);
    }
}
